﻿using System.AutoUpdate.Interfaces;
using System.AutoUpdate.Library;
using System.Collections.Generic;

namespace System.AutoUpdate.Core.Sources
{
    /// <summary>
    /// Represents a simple source with no functionality
    /// </summary>
    public abstract class SimpleSource : IUpdateSource
    {
        /// <summary>
        /// Holds available versions mapped to it's location objects
        /// </summary>
        protected Dictionary<UpdateVersionInfo, object> AvailableVersions
        {
            get;
            private set;
        }

        /// <summary>
        /// Returns list of versions
        /// </summary>
        /// <returns></returns>
        public List<UpdateVersionInfo> GetVersions ()
        {
            return new List<UpdateVersionInfo> (AvailableVersions.Keys);
        }

        /// <summary>
        /// Creates simple source
        /// </summary>
        protected SimpleSource ()
        {
            AvailableVersions = new Dictionary<UpdateVersionInfo, object> ();
        }

        /// <summary>
        /// Returns true if version is available
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool HasVersion (UpdateVersionInfo info)
        {
            return (AvailableVersions.ContainsKey (info));
        }

        /// <summary>
        /// Executes transfer event
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void TransferEvent (TransferEvent eventType, object sender, TransferEventArgs eventArgs)
        {
            switch (eventType)
            {
                case Core.TransferEvent.Started:
                    if (TransferStarted != null)
                    {
                        TransferStarted (sender, eventArgs);
                    }

                    break;
                case Core.TransferEvent.Progress:
                    if (TransferProgressChanged != null)
                    {
                        TransferProgressChanged (sender, eventArgs);
                    }

                    break;
                case Core.TransferEvent.Completed:
                    if (TransferEnded != null)
                    {
                        TransferEnded (sender, eventArgs);
                    }

                    break;
            }
        }

        #region Abstract Methods

        public IVersionParser VersionParser
        {
            get;
            set;
        }

        public event TransferEventHandler TransferStarted;
        public event TransferEventHandler TransferProgressChanged;
        public event TransferEventHandler TransferEnded;

        protected abstract void LoadVersions ();
        
        public abstract void Transfer (UpdateVersionInfo info);
        public abstract bool CheckSignature (UpdateVersionInfo info, bool online = false);
        #endregion
    }
}
